var http = require('../../../utils/https.js');
import Toast from '../../../miniprogram_npm/@vant/weapp/toast/toast';
Page({
  data: {

    codeImg: "…vhv8A4qo7nxnqN1azW7w2oSVGRiqtkAjHHzVMq0bMUqsbM//Z",
    imgUrl: "data:image/png;base64,",
    icnoP: 'https://wqqqf.oss-cn-beijing.aliyuncs.com/921416cd12798ad85b0d542ff86bf7bb.png',
    icnoC: 'https://wqqqf.oss-cn-beijing.aliyuncs.com/7ff3df4d3bdf4f653fa2f39499169c6d.png',
    uuid: '',
    code: '',
    username: '',
    password: '',
    token: '',
    error_username:'',
    error_password:'',
    error_code:'',
  },
  isAlphanumeric(value) {
    const reg = /^[A-Za-z0-9]+$/;
    return reg.test(value);
  },
  login() {
    var username = this.data.username
    var password = this.data.password
    var code = this.data.code

    if (this.isAlphanumeric(username) && this.isAlphanumeric(password) && this.isAlphanumeric(code)) {
      var that = this
      http.POST('/login', {
        username: that.data.username,
        password: that.data.password,
        uuid: that.data.uuid,
        code: this.data.code
      }, function (res) {
        if (res.code === 200) {
          that.setData({
            token: res.token
          })
          wx.setStorageSync('token', res.token)
          console.log(that.data.token);
          http.GET('/getInfo', {}, function (res) {
            if (res.code === 200) {
              wx.setStorageSync('userInfo', res.user)
              wx.reLaunch({
                url: '/pages/index/index',
              })
            } else {

            }
          }, function (err) {
            console.error('失败:', err);
          });
        } else {
          Toast.fail({
            message: res.msg,
            forbidClick: true,
          });
          that.setData({
            username: '',
            password: '',
            code: '',
            uuid: ''
          })
          that.updateCode()
        }
      }, function (err) {
        console.error('失败:', err);
      });
    }else{
      if(!this.isAlphanumeric(username)){
        this.setData({
          error_username:'账号格式不正确'
        })
      }
      if(!this.isAlphanumeric(password)){
        this.setData({
          error_password:'密码由数字字符组成'
        })
      }
      if(!this.isAlphanumeric(code)){
        this.setData({
          error_code:'验证码格式不正确'
        })
      }
    }


  },
  updateCode() {
    var img = 'data:image/png;base64,'
    var t = Date.now()
    var that = this
    http.GET('/captchaImage', {}, function (res) {
      img = img + res.img
      that.setData({
        imgUrl: img,
        uuid: res.uuid
      })
      console.log('成功:', res);
    }, function (err) {
      console.error('失败:', err);
    });
  },

  onChangeU(event) {
    // event.detail 为当前输入的值
    this.setData({
      username: event.detail
    })
  },

  onChangeP(event) {
    // event.detail 为当前输入的值
    // console.log("P",event.detail);
    this.setData({
      password: event.detail
    })
  },

  onChangeC(event) {
    // event.detail 为当前输入的值
    // console.log("C",event.detail);
    this.setData({
      code: event.detail
    })
  },
  after() {
    console.log(this.data.token);
    wx.removeStorageSync('token')
  },
  register(){
    wx.navigateTo({
      url: '/index/pages/mzhRegister/mzhRegister',
    })
  },
  onLoad(options) {
    this.updateCode()
    var token = wx.getStorageSync('token')
    console.log(token);
  },
})